package com.mm.serivce.taxi.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.domain.taxi.DCarInfo;
import com.mm.domain.taxi.DMiniUserInfo;
import com.mm.mapper.taxi.DCarInfoMapper;
import com.mm.mapper.taxi.DMiniUserInfoMapper;
import com.mm.serivce.taxi.IDCarInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 车辆信息表 服务实现类
 * </p>
 *
 * @author JueDream
 * @since 2021-08-13
 */
@Service
public class DCarInfoServiceImpl extends ServiceImpl<DCarInfoMapper, DCarInfo> implements IDCarInfoService {

    private final DCarInfoMapper carInfoMapper;

    private final DMiniUserInfoMapper miniUserInfoMapper;

    public DCarInfoServiceImpl(DCarInfoMapper carInfoMapper, DMiniUserInfoMapper miniUserInfoMapper) {
        this.carInfoMapper = carInfoMapper;
        this.miniUserInfoMapper = miniUserInfoMapper;
    }

    @Override
    public Page<DCarInfo> queryCarPage(Integer page, Integer limit, String carText) {
        Page<DCarInfo> carIPage = new Page<>(page, limit);
        LambdaQueryWrapper<DCarInfo> queryWrapper = new LambdaQueryWrapper<DCarInfo>();
        if (StrUtil.isNotBlank(carText)) {
            queryWrapper.like(DCarInfo::getCarName, carText);
        }
        queryWrapper.eq(DCarInfo::getDelFlg, '0');
        carIPage = carInfoMapper.selectPage(carIPage, queryWrapper);
        carIPage.setTotal(carIPage.getRecords().size());
        // 司机名称
        Optional.ofNullable(carIPage.getRecords()).orElse(new ArrayList<>())
                .forEach(c -> {
                    if (StrUtil.isNotBlank(c.getOpenId())){
                        LambdaQueryWrapper<DMiniUserInfo> queryMWrapper = new LambdaQueryWrapper<DMiniUserInfo>();
                        queryMWrapper.eq(DMiniUserInfo::getOpenId, c.getOpenId());
                        DMiniUserInfo miniUserInfo = miniUserInfoMapper.selectOne(queryMWrapper);
                        if (Objects.nonNull(miniUserInfo)) {
                            c.setMiniName(miniUserInfo.getMName());
                        }
                    }
                });
        return carIPage;
    }

    @Override
    public DCarInfo queryCarOne(Integer carId) {
        DCarInfo carInfo = new DCarInfo();
        if (carId > 0){
            carInfo = carInfoMapper.selectById(carId);
        }
        return carInfo;
    }

    @Override
    public boolean removeCar(Integer carId) {
        boolean flag = false;
        DCarInfo carInfo = carInfoMapper.selectById(carId);
        if (Objects.nonNull(carInfo)) {
            carInfo.setDelFlg(1);
            Integer result = carInfoMapper.updateById(carInfo);
            if (result > 0) {
                flag = true;
            }
        }
        return flag;
    }
}
